//
// Created by yfux on 2022/4/18.
//

#ifndef ICANLIVE_ICANPLAYER_H
#define ICANLIVE_ICANPLAYER_H


#include <pthread.h>
#include <android/native_window.h>
#include "JavaCallHelper.h"
#include "VideoPlayer.h"
#include "AudioPlayer.h"

extern "C" {
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
};

class ICanPlayer {
    friend void *prepare_t(void *args);

    friend void *start_t(void *args);

public:
    void setDataSource(const char *path);

    void prepare();

    ICanPlayer();

    ICanPlayer(JavaCallHelper *javaCallHelper);

    void start();

    void setWindow(ANativeWindow *window);

    void stop();

private:
    void _prepare();

    void _start();

private:
    char *path;
    pthread_t prepareThread;
    pthread_t startThread;
    AVFormatContext *avFormatContext;
    JavaCallHelper *javaCallHelper;
    int64_t duration;
    VideoPlayer *videoPlayer;
public:
    virtual ~ICanPlayer();

private:
    AudioPlayer *audioPlayer;
    bool isPlaying = false;
    ANativeWindow *window = NULL;
    int width, height;

    void release();
};


#endif //ICANLIVE_ICANPLAYER_H
